<?php
@ob_start();
@session_start();

if($_SESSION['user_username']=="" && $_SESSION["user_name"]=="" && $_SESSION["user_surname"]==""){
	echo "<script>window.location = '../index.php'</script>";
}
include('../function/config.php');
include("../connect/connect.php");
include('../function/thaidate.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-autocomplete/lib/jquery.js"></script>
<script type="text/javascript" src="jquery-autocomplete/jquery.autocomplete.js"></script>
<script type="text/javascript" src="jquery-autocomplete/lib/jquery.bgiframe.min.js"></script>
<script type="text/javascript" src="jquery-autocomplete/lib/jquery.ajaxQueue.js"></script>
<script type="text/javascript" src="jquery-autocomplete/lib/thickbox-compressed.js"></script>
<link rel="stylesheet" href="jquery-autocomplete/lib/thickbox.css" />
<link rel="stylesheet" href="jquery-autocomplete/jquery.autocomplete.css"/>
<title><?php echo $config['title'];?></title>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
<style type="text/css">
<!--
.style1 {font-size: 13px}
-->
</style>
</head>
  <?php
$Query = "SELECT prod_name FROM product WHERE prod_status = '0' ";
$objQuery = mysql_query($Query) or die("Error Query [" . $Query . "]");
	while($row = mysql_fetch_assoc($objQuery)){
		$line1[]=array($row["prod_name"]);
	}
?> 
<script type="text/javascript">
$(document).ready(function(){

	var str_month = <?php echo json_encode($line1).';'; ?>

	$("#input").autocomplete( str_month , {
		mustMatch: false,
		autoFill: false,
		matchContains:true,
		});
<?php if($_SESSION["position_id"]=='1'){?>
	var delayID=null;
	$("#member_code").keyup(function(event){
		if(delayID==null){
			delayID=setTimeout(function(){
				var input_data=$("#member_code").val();
				alert('กรุณาใช้เครื่องยิงบาร์โค้ดเท่านั้น!!');
				setTimeout("location.href = 'manage_list.php';",100);
				delayID=null;
			},100);
		}else{
			if(delayID){
				clearTimeout(delayID);
				delayID=setTimeout(function(){
					var input_data=$("#member_code").val();
					alert('กรุณาใช้เครื่องยิงบาร์โค้ดเท่านั้น!!');
					setTimeout("location.href = 'manage_list.php';",100);
					delayID=null;
				},100);
			}		
		}
	});
<?php } ?>
})
</script>
<?php

//============ กรณีคิดค่าบริการ
if(trim($_GET["action"]) == "service")
{
	$objQuerySv = mysql_query("UPDATE table_status SET table_status_sv='1' WHERE table_status_table = '".$_SESSION["table"]."' ");
	header('Location: manage_list.php');
}

//============ กรณียกเลิกค่าบริการ
if(trim($_GET["action"]) == "noservice")
{
	$objQuerySv = mysql_query("UPDATE table_status SET table_status_sv='0' WHERE table_status_table = '".$_SESSION["table"]."' ");
	header('Location: manage_list.php');
}

//============ ถ้ามีการเลือกรายการสินค้าให้เก็บลง db
if(trim($_POST["input"]) != "")
{
	$Querycode = "SELECT prod_barcode,prod_name FROM product WHERE prod_status = '0' AND prod_barcode = '".$_POST["input"]."' OR prod_name = '".$_POST["input"]."'";
	$dbquerycode = mysql_query($Querycode);
	$resultcode = mysql_fetch_array($dbquerycode);
	
	if($resultcode["prod_barcode"]==$_POST["input"] ||  $resultcode["prod_name"]==$_POST["input"]){
	$Query = "INSERT INTO  sale_session (sale_session_prod,sale_session_qty,sale_session_table) VALUES ('".$resultcode["prod_barcode"]."','".$_POST["quantity"]."','".$_SESSION["table"]."')";
	$objQuery = mysql_query($Query);
	} else {
	$nocode = "<center><strong><font color=red><br>กรุณากรอกรหัสสินค้าให้ถูกต้อง</font></strong></center>";	
	}
}

//============ กรณีลบรายการสั่งซื้อ
if(trim($_GET["action"]) == "Del")
{
	$Query = "DELETE FROM sale_session WHERE sale_session_prod = '".$_GET['Code']."' AND sale_session_table = '".$_SESSION["table"]."'";
	$objQuery = mysql_query($Query);
	header('Location: manage_list.php');
}

//============ กรณีลบรหัสลูกค้า
if(trim($_GET["action"]) == "Deluser")
{
	mysql_query("DELETE FROM member_non_session WHERE member_non_session_table = '".$_SESSION["table"]."'");
	header('Location: manage_list.php');
}

//============ กรณีลบรหัสสมาชิก
if(trim($_GET["action"]) == "DelMember")
{
	mysql_query("DELETE FROM member_session WHERE member_session_table = '".$_SESSION["table"]."'");
	header('Location: manage_list.php');
}

//============ กรณีจ่ายเงิน
if(trim($_GET["action"]) == "receivem")
{
	$Query = "UPDATE table_status SET receive_money = '".$_POST['show']."' WHERE table_status_table = '".$_SESSION["table"]."'";
	$objQuery = mysql_query($Query);

	header("location:$_SERVER[PHP_SELF]");
}

//============ กรณียกเลิกรายการ
if(trim($_GET["action"]) == "listcancel")
{
//ลบเลขบิล
/*
	$QuerySBill = mysql_query("SELECT bill_id FROM table_status WHERE table_status_table = '".$_SESSION["table"]."'");
	$rowbill = mysql_fetch_array($QuerySBill);
	$QueryBill = mysql_query("DELETE FROM bill WHERE bill_id = '".$rowbill['bill_id']."'");
*/

//ลบ Session การขาย
	$Query = "DELETE FROM sale_session WHERE sale_session_table = '".$_SESSION["table"]."'";
	$objQuery = mysql_query($Query);

//ลบ Session โต๊ะ	
	mysql_query("DELETE FROM table_status WHERE table_status_table = '".$_SESSION["table"]."'");

//ลบ session สมาชิก
	$Query1 = "DELETE FROM member_session WHERE member_session_table = '".$_SESSION["table"]."'";
	$objQuery1 = mysql_query($Query1);

	$Query21 = "DELETE FROM member_non_session WHERE member_non_session_table = '".$_SESSION["table"]."'";
	$objQuery21 = mysql_query($Query21);

	header("location:index.php");
}

//============ กรณีพิมพ์รหัสสมาชิก
if($_POST['member_code'] != "")
{
	$Querycode = "SELECT member_code,memtype_id,member_date_start,member_date_end FROM member WHERE member_code = '".$_POST["member_code"]."'";
	$dbquerycode = mysql_query($Querycode);
	$resultcode = mysql_fetch_array($dbquerycode);
	$membercode = $resultcode["member_code"];
	$rbrow = mysql_num_rows($dbquerycode);

	if($rbrow >= 1){
		if($resultcode['memtype_id']=='4'){
			header("location: add_name_ar.php?code=$membercode");
		} else if($resultcode['memtype_id']=='5'){
			header("location: add_name.php?code=$membercode");
		} else if($resultcode['memtype_id']=='6'){
			header("location: add_name.php?code=$membercode");
		} else if($resultcode['memtype_id']=='7'){
			header("location: add_name.php?code=$membercode");
		}

		if($resultcode['memtype_id']<='3'){
			$dateend = DateDiff(date("Y-m-d"),$resultcode['member_date_end']);
			if($dateend > 0){

				$Query = "INSERT INTO  member_session (member_session_code,member_session_table) VALUES ('".$resultcode["member_code"]."','".$_SESSION["table"]."') ON DUPLICATE KEY UPDATE member_session_code='".$resultcode["member_code"]."'";
				$objQuery = mysql_query($Query);

				$Query1 = "DELETE FROM member_non_session WHERE member_non_session_table = '".$_SESSION["table"]."'";
				$objQuery1 = mysql_query($Query1);
			} else {
				echo "<script type=\"text/javascript\">alert('บัตรสมาชิกนี้หมดอายุแล้วค่ะ')</script>";
			}
		} else {
			echo "<script type=\"text/javascript\">alert('ไม่มีรหัสสมาชิกนี้ค่ะ')</script>";
		}

	} else {
		echo "<script type=\"text/javascript\">alert('ไม่มีรหัสสมาชิกนี้ค่ะ')</script>";
	}
}
?>
<script language="javascript">
function IsNumeric(sText,obj)
{
	var ValidChars = "0123456789.";
	var IsNumber=false;
	var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      {
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   		if(IsNumber==false){
			obj.value=sText.substr(0,sText.length-1);
		}
}

function setFocus(){
document.sale.input.focus();
}

function open_popup(){

	if(confirm('ยืนยันการพิมพ์ใบเสร็จ')==true)
	{
	window.open('bill.php',null,"height=550,width=470,status=no,toolbar=no,menubar=no,location=no,scrollbars=1");
	//window.location.href='manage_list.php';
	setTimeout("location.href = 'manage_list.php';",1000);
	}
}

</script>
<body class="sale" onload="setFocus()" onpaste="return false"; >
<div class="sale-all">
<div class="headtable"><form id="form1" name="form1" method="post" action=""> <b>โต๊ะที่ : </b> <?php echo $_SESSION["table"]; ?>&nbsp;&nbsp;<a href="javascript:if(confirm('ยืนยันการเปลี่ยนหมายเลขโต๊ะ')==true){window.location='edit_list.php?table=<?php echo $_SESSION["table"]; ?>';}"><img src="image/Play-Mode-Repeat-All-Hot-icon.png" width="20" height="20" title="เปลี่ยนหมายเลขโต๊ะ" border="0" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
<b>รหัสลูกค้า : </b>
  <input name="member_code" type="text" id="member_code" autocomplete="off" />
  &nbsp;&nbsp;
<?php
	$Query = "SELECT member.member_name as member_name, member.member_surname as member_surname,membertype.memtype_discount as memtype_discount FROM member,member_session,membertype WHERE membertype.memtype_id = member.memtype_id AND member.member_code=member_session.member_session_code AND member_session.member_session_table='".$_SESSION["table"]."'";
	$dbquery = mysql_query($Query);
	$result = mysql_fetch_assoc($dbquery);
	if($result){
	echo "<b>ชื่อสมาชิก :</b> ".$result['member_name']." ".$result['member_surname'];
?>
&nbsp;&nbsp;<a href="javascript:if(confirm('ลบรหัสลูกค้า')==true){window.location='<?php echo $_SERVER['PHP_SELF'];?>?action=DelMember';}"><img src="image/Button-Delete-icon.png" title="ลบรหัสสมาชิก" border="0" /></a>
<?php 
	}
	
	$Query1 = "SELECT member_non_session.member_non_session_name as member_non_session_name,member_non_session.member_non_session_phone as member_non_session_phone,member_non_session.member_non_session_membertype as member_non_session_membertype,membertype.memtype_name as memtype_name FROM member_non_session,member,membertype WHERE member_non_session.member_non_session_code = member.member_code AND member.memtype_id = membertype.memtype_id AND member_non_session.member_non_session_table ='".$_SESSION["table"]."'";
	$dbquery1 = mysql_query($Query1);
	$result1 = mysql_fetch_assoc($dbquery1);
	
	if($result1){
	echo "<b>ชื่อ :</b> ".$result1['member_non_session_name']."&nbsp;&nbsp;&nbsp; <b>ประเภท :</b> ".$result1['memtype_name'];
?>
&nbsp;&nbsp;<a href="javascript:if(confirm('ลบรหัสลูกค้า')==true){window.location='<?php echo $_SERVER['PHP_SELF'];?>?action=Deluser';}"><img src="image/Button-Delete-icon.png" title="ลบรหัสลูกค้า" border="0" /></a>
<?php
	}
?>
</form>
</div>
<div id="scroll_product"><table class="listsale">
<thead>
  <tr class="headlist">
    <td width="4%">ลำดับ</td>
    <td width="10%">รหัสสินค้า</td>
    <td>รายการสินค้า</td>
    <td width="8%">ราคา</td>
    <td width="6%">จำนวน</td>
    <td width="10%">รวม</td>
    <td width="9%">ค่าบริการ</td>
    <td width="8%">ส่วนลด</td>
    <td width="10%">รวมสุทธิ</td>
    <td width="4%">ลบ</td>
  </tr>
  </thead>
<?php

//============ ทำการแสดงรายการที่ได้เลือกว่ามีกี่รายการ

$strNum=0;
$strTotal=0;
$cost=0;

//============ แสดงจำนวนรายการสินค้า
	$Query = "SELECT sale_session.sale_session_id as sale_session_id,sale_session.sale_session_prod as sale_session_prod,sum(sale_session.sale_session_qty) as sale_session_qty,sale_session.sale_session_table as sale_session_table,product.prod_barcode as prod_barcode,product.prod_name as prod_name,product.prod_price as prod_price,product.prodtype_id as prodtype_id FROM product,sale_session WHERE product.prod_barcode=sale_session.sale_session_prod AND sale_session.sale_session_table='".$_SESSION["table"]."' GROUP BY sale_session.sale_session_prod ORDER BY sale_session.sale_session_id ASC";
	$dbquery = mysql_query($Query);
	
	while($result= mysql_fetch_array($dbquery))
		{
		$strNum++;
?>
  <tr>
    <td align="center"><?php echo $strNum;?></td>
    <td align="left" style="padding-left:10px"><?php echo $result['prod_barcode'];?></td>
    <td align="left" style="padding-left:10px"><?php echo $result['prod_name'];?></td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($result['prod_price'], 2,'.',',');?></td>
    <td align="right" style="padding-right:20px"><?php echo $result['sale_session_qty'];?></td>
    <td align="right" style="padding-right:10px"><?php $tol = $result["prod_price"]*$result['sale_session_qty']; echo number_format($tol, 2,'.',',');?></td>
    <td align="right" style="padding-right:10px">
<?php
$Query11 = mysql_query("SELECT table_status_sv FROM table_status WHERE table_status_table='".$_SESSION["table"]."'");
$result11 = mysql_fetch_array($Query11);

//คิดค่าบริการ 10%
if($result['prodtype_id']=="5"){
	$dc = "0";
}else if($result11['table_status_sv']=='1'){

	$dc = round(0.1*$tol);
	//echo $result11['memtype_discount'];
} else if($result11['table_status_sv']=='0') {
	$dc = "0";
}
	echo number_format($dc, 2,'.',',');
?>
	</td>
    <td align="right" style="padding-right:10px">
<?php
//คิดส่วนลด
$mQuery = mysql_query("SELECT membertype.memtype_discount as memtype_discount FROM member_session,member,membertype WHERE member_session.member_session_code = member.member_code AND member.memtype_id = membertype.memtype_id AND member_session.member_session_table='".$_SESSION["table"]."' ");
$mResult = mysql_fetch_array($mQuery);
if($mResult){
	if($result['prodtype_id']=="3"){
		$dis = '0';
	} else if($result['prodtype_id']=="4"){
		$dis = '0';
	} else if($result['prodtype_id']=="5"){
		$dis = '0';
	} else {
		$dis = round((1-$mResult['memtype_discount'])*$tol);
	}
	echo number_format($dis, 2,'.',',');
} else {
	echo "0.00";
}
?>
    </td>
    <td align="right" style="padding-right:10px">
<?php
	
	$total2 = ($tol+$dc)-$dis;
/*
	if($result11['table_status_sv']=='1'){
	$total2 = $discount+($dc);
	} else if($result11['table_status_sv']=='0') {
	$total2 = $result['prod_price']*$result['sale_session_qty'];
	}
*/
	echo "".number_format($total2, 2,'.',',');
?>
	</td>
    <td align="center"><a href="javascript:if(confirm('ลบรายการสินค้า')==true){window.location='<?php echo $_SERVER['PHP_SELF'];?>?action=Del&amp;Code=<?php echo $result['sale_session_prod'];?>';}"><img src="image/Close-2-icon.png" title="ลบรายการสินค้า" width="15" height="15" border="0" /></a></td>
  </tr>
  <?php 
		$strTotal=$strTotal+$total2;
		}
//============ กรณีบันทึกรายการ
if(trim($_GET["action"]) == "savelist")
{
	$Query = "SELECT sale_session.sale_session_id as sale_session_id,sale_session.sale_session_prod as sale_session_prod,sum(sale_session.sale_session_qty) as sale_session_qty,sale_session.sale_session_table as sale_session_table,product.prod_barcode as prod_barcode,product.prod_name as prod_name,product.prod_price as prod_price,product.prod_id as prod_id,product.prodtype_id as prodtype_id FROM product,sale_session WHERE product.prod_barcode=sale_session.sale_session_prod AND sale_session.sale_session_table='".$_SESSION["table"]."' GROUP BY sale_session.sale_session_prod ORDER BY sale_session.sale_session_id ASC";
	$dbquery = mysql_query($Query);


	$Query12 = "SELECT member.member_id as member_id,member.memtype_id as memtype_id FROM member,member_session WHERE member.member_code=member_session.member_session_code AND member_session.member_session_table='".$_SESSION["table"]."'";
	$dbquery12 = mysql_query($Query12);
	$result12 = mysql_fetch_assoc($dbquery12);
	$row12 = mysql_num_rows($dbquery12);


	$Query122 = "SELECT member.member_id as member_id,member.memtype_id as memtype_id FROM member,member_non_session WHERE member.member_code=member_non_session.member_non_session_code AND member_non_session.member_non_session_table='".$_SESSION["table"]."'";
	$dbquery122 = mysql_query($Query122);
	$result122 = mysql_fetch_assoc($dbquery122);
	$row122 = mysql_num_rows($dbquery122);

	if ($row12>='1'){
		$member_id = $result12['member_id'];
	}
	if ($row122>='1'){
		$member_id = $result122['member_id'];
	}

	if($row12>='1' || $row122>='1'){
		if($result12['memtype_id']=='1' || $result122['memtype_id']=='1'){
			$saletype = '2';
		} else if($result12['memtype_id']=='2' || $result122['memtype_id']=='2') {
			$saletype = '6';
		}  else if($result12['memtype_id']=='7' || $result122['memtype_id']=='7') {
			$saletype = '7';
		}  else if($result12['memtype_id']=='4' || $result122['memtype_id']=='4') {
			$saletype = '3';
		} else if($result12['memtype_id']=='5' || $result122['memtype_id']=='5') {
			$saletype = '4';
		} else if($result12['memtype_id']=='6' || $result122['memtype_id']=='6') {
			$saletype = '5';
		} else if($result12['memtype_id']=='3' || $result122['memtype_id']=='3') {
			$saletype = '8';
		}
	} else {
		$saletype = '1';
	}

	$Stable = mysql_query("SELECT bill_id FROM table_status WHERE table_status_table = '".$_SESSION["table"]."'");
	$Bill = mysql_fetch_array($Stable);

	$time = time();
	$time2 = date("Y-m-d H:i:s");

	$d = date("H", $time);

	$time3 = date("Y-m-d",  $time);

	if($saletype=='3'){
		//บันทึกข้อมูลลง debtor_total
		$QueryDebTo = mysql_query("INSERT INTO debtor_total (debtor_total_date,debtor_total_date2,debtor_total_date3,debtor_total_name,debtor_total_phone,debtor_total_total,bill_id,debtortype_id,user_id) VALUES ('".$time."','".$time2."','".$time3."','".$result1["member_non_session_name"]."','".$result1["member_non_session_phone"]."','".$strTotal."','".$Bill['bill_id']."','".$result1["member_non_session_membertype"]."','".$_SESSION['user_id']."')");
	}
		
	while($result= mysql_fetch_array($dbquery))
	{
		$tol1511 = $result["prod_price"]*$result['sale_session_qty'];
		
		//คิดค่าบริการเก็บลงตาราง sale
		$Query1511 = mysql_query("SELECT table_status_sv FROM table_status WHERE table_status_table='".$_SESSION["table"]."'");
		$result1511 = mysql_fetch_array($Query1511);

		//คิดค่าบริการ 10%
		if($result['prodtype_id']=='5'){
			$svcharge = "0";
		} else if($result1511['table_status_sv']=='1'){
			$svcharge = round(0.1*$tol1511);
		} else if($result1511['table_status_sv']=='0') {
			$svcharge = "0";
		}

		//คิดส่วนลดเก็บลงตาราง sale
		$mQuery1511 = mysql_query("SELECT membertype.memtype_discount as memtype_discount FROM member_session,member,membertype WHERE member_session.member_session_code = member.member_code AND member.memtype_id = membertype.memtype_id AND member_session.member_session_table='".$_SESSION["table"]."' ");
		$mResult1511 = mysql_fetch_array($mQuery1511);
		if($mResult1511){
			if($result['prodtype_id']=="3"){
				$discount = '0';
			} else if($result['prodtype_id']=="4"){
				$discount = '0';
			} else if($result['prodtype_id']=="5"){
				$discount = '0';
			} else {
				$discount = round((1-$mResult1511['memtype_discount'])*$tol1511);
			}
		} else {
			$discount = '0';
		}

		if($saletype=='3'){
			//เลือกรหัสล่าสุดมาใช้อ้างอิงเก็บรหัสตารางลูกหนี้หลัก
			$sDebtor = mysql_query("SELECT max(debtor_total_id) as debtor_total_id FROM debtor_total");
			$NumDebtor = mysql_fetch_array($sDebtor);
			$nDebtor = $NumDebtor['debtor_total_id'];
				
			//บันทึกลงตาราง debtor
			$QueryDebtor = mysql_query("INSERT INTO debtor (prod_id,sale_qty,sale_price,sale_service,debtor_total_id) VALUES ('".$result["prod_id"]."','".$result["sale_session_qty"]."','".$result["prod_price"]."','".$svcharge."','".$nDebtor."')");
		}

		//บันทึกลงตาราง sale
		$Query = "INSERT INTO  sale (sale_datetime,sale_datetime3,stype_id,member_id,prod_id,sale_qty,sale_price,sale_discount,sale_service,cus_name,user_id,bill_id) VALUES ('".$time."','".$time3."','".$saletype."','".$member_id."','".$result["prod_id"]."','".$result["sale_session_qty"]."','".$result["prod_price"]."','".$discount."','".$svcharge."','".$result1['member_non_session_name']."','".$_SESSION['user_id']."','".$Bill['bill_id']."')";
		$objQuery = mysql_query($Query);

		//บันทึกลงตาราง sale_report
		$Query22 = mysql_query("INSERT INTO  sale_report (sale_datetime,stype_id,member_id,prod_id,sale_qty,sale_price,sale_discount,sale_service,cus_name,user_id,bill_id) VALUES ('".$time."','".$saletype."','".$member_id."','".$result["prod_id"]."','".$result["sale_session_qty"]."','".$result["prod_price"]."','".$discount."','".$svcharge."','".$result1['member_non_session_name']."','".$_SESSION['user_id']."','".$Bill['bill_id']."')");

		//บันทึกลงตาราง sale_report2
		$Query2332 = mysql_query("INSERT INTO  sale_report2 (sale_datetime,stype_id,member_id,prod_id,sale_qty,sale_price,sale_discount,sale_service,cus_name,user_id,bill_id) VALUES ('".$time."','".$saletype."','".$member_id."','".$result["prod_id"]."','".$result["sale_session_qty"]."','".$result["prod_price"]."','".$discount."','".$svcharge."','".$result1['member_non_session_name']."','".$_SESSION['user_id']."','".$Bill['bill_id']."')");
	}

	//ตัดสต็อก
	$Query19 = "SELECT sale_session_prod,sum(sale_session_qty) as sale_session_qty FROM sale_session WHERE sale_session_table = '".$_SESSION["table"]."' GROUP BY sale_session_prod";
	$dbquery19 = mysql_query($Query19);

	while($result19= mysql_fetch_array($dbquery19))
	{
		$Quanlity = $result19["sale_session_qty"];
		$sql = "update product set prod_quantity = prod_quantity-$Quanlity where prod_barcode = '".$result19["sale_session_prod"]."'";
		$dbquery = mysql_query($sql);

		/*
		 //เพิ่มจำนวนขายในสต็อกบาร์
		 $QueryBar = mysql_query("SELECT stock_bar.stock_bar_id FROM product,stock_bar WHERE product.prod_id = stock_bar.prod_id AND product.prod_barcode = '".$result19["sale_session_prod"]."'");
		 $RowBar = mysql_fetch_array($QueryBar);

		 mysql_query("UPDATE stock_bar_cut SET stock_bar_cut_sale = stock_bar_cut_sale + $Quanlity WHERE stock_bar_id = '".$RowBar['stock_bar_id']."' ");
		 */
	}

	/*
	 //เพิ่ม point
	 $Query11 = "SELECT member.member_code as member_code FROM member,member_session WHERE member.member_code=member_session.member_session_code AND member_session.member_session_table='".$_SESSION["table"]."'";
	 $dbquery11 = mysql_query($Query11);
	 $result11 = mysql_fetch_assoc($dbquery11);

	 $sql1 = "update member set member_point = member_point+$strTotal where member_code = '".$result11['member_code']."'";
	 $dbquery1 = mysql_query($sql1);
	 */

	//ลบ session โต๊ะ
	$Query = "DELETE FROM sale_session WHERE sale_session_table = '".$_SESSION["table"]."'";
	$objQuery = mysql_query($Query);

	//ลบ session สมาชิก
	$Query1 = "DELETE FROM member_session WHERE member_session_table = '".$_SESSION["table"]."'";
	$objQuery1 = mysql_query($Query1);

	$Query21 = "DELETE FROM member_non_session WHERE member_non_session_table = '".$_SESSION["table"]."'";
	$objQuery21 = mysql_query($Query21);

	//ลบสถานะโต๊ะ
	mysql_query("DELETE FROM table_status WHERE table_status_table = '".$_SESSION["table"]."'");

	header("location: index.php");

}
?>
</table>
<?php
	echo $nocode;
			if($strNum==0)
				{
				echo "<br><center><strong><font color=red>ไม่มีรายการสินค้า</font></strong></center>";
				}
?></div>
<div class="control">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><form action="" method="post" name="sale">
<table class="input" width="240" border="0" align="left" cellpadding="0" cellspacing="0">
  <tr>
    <td class="coloradd">รหัสสินค้า</td>
  </tr>
  <tr>
    <td width="240"><div align="left">
      <input name="input" id="input" type="text" style="width:200px; text-align:center; height:30px; font-size:25px" autocomplete="off"/>
    </div></td>
  </tr>
  <tr>
    <td class="input">จำนวน</td>
  </tr>
  <tr>
    <td><div align="left">
      <input name="quantity" type="text" id="quantity" style="width:100px; text-align:center; height:30px; font-size:25px" value="1" autocomplete="off"/>
      <input type="submit" name="Submit" value="เพิ่ม" style="width:90px; height:37px; text-align:center; font-size:20px" />
    </div>
      </td>
  </tr>
</table>
</form></td>
    <td>
  <input type="button" name="Submit2" value="เช็คบิล" style="width:140px; height:40px; font-size:18px" onclick="open_popup();" <?php if($strNum==0){ echo "disabled"; } ?> />
  <input type="button" name="Submit22" value="จ่ายแล้ว"
						onclick="javascript:if(confirm('ยืนยันการจ่ายเงิน')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=savelist';}"
						style="width: 140px; height: 40px; font-size: 18px"
<?php
//เช็คว่ากดเช็คบิลแล้วหรือยัง
$BillQuery = mysql_query("SELECT table_status_group FROM table_status WHERE table_status_group = '0' AND table_status_table = '".$_SESSION["table"]."'");
$BillNum = mysql_num_rows($BillQuery);

$Query1 = "SELECT member_non_session_id FROM member_non_session WHERE member_non_session_table ='".$_SESSION["table"]."'";
$dbquery1 = mysql_query($Query1);
$result1 = mysql_num_rows($dbquery1);

$receive = "SELECT receive_money FROM table_status WHERE table_status_table = '".$_SESSION["table"]."'";
$receivequery = mysql_query($receive);
$receiverow = mysql_fetch_assoc($receivequery);

if($result1>0){
	echo "disabled=\"disabled\"";
} else if($receiverow['receive_money']<$strTotal){
	echo "disabled=\"disabled\"";
} else if($strNum==0){
	echo "disabled=\"disabled\"";
} else if($BillNum>='1'){
	echo "disabled=\"disabled\"";
}
?> />

						<br />
  <input name="cancle" type="button" id="cancle" value="ยกเลิกรายการ" onclick="javascript:if(confirm('ยืนยันการยกเลิกรายการ')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=listcancel';}" style="width:140px; height:40px; font-size:18px"/>
  <input name="savelist" type="button" id="savelist" value="บันทึกรายการ" onclick="javascript:if(confirm('ยืนยันการบันทึกรายการ')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=savelist';}" style="width:140px; height:40px; font-size:18px" <?php 
   
	if($BillNum>='1'){
		echo "disabled=\"disabled\"";		
	} else {
		$cMemNo = mysql_query("SELECT * FROM member_non_session WHERE member_non_session_table = '".$_SESSION["table"]."'");
		$NumMemNo = mysql_num_rows($cMemNo);
		
		$cMem = mysql_query("SELECT * FROM member_session WHERE member_session_table = '".$_SESSION["table"]."'");
		$NumMem = mysql_num_rows($cMem);
		
		if($NumMemNo<='0' && $NumMem<='0'){
			echo "disabled=\"disabled\"";
		} else if($NumMemNo=='0'){
			echo "disabled=\"disabled\"";
		}
	}
	
 /*  
    $Query = "SELECT * FROM member,member_session WHERE member.member_code=member_session.member_session_code AND member_session.member_session_table='".$_SESSION["table"]."'";
	$dbquery = mysql_query($Query);
	$result = mysql_num_rows($dbquery);
	
	$Query1 = "SELECT * FROM member_non_session WHERE member_non_session_table ='".$_SESSION["table"]."'";
	$dbquery1 = mysql_query($Query1);
	$result1 = mysql_num_rows($dbquery1);

	if($result>0){
		echo "disabled";
	} else if($strNum==0){
		echo "disabled";
	} else if($result==0 && $result1==0){
		echo "disabled";
	}
*/
	 ?>/>
  <br />
  <input name="table" type="button" id="table" value="โต๊ะ" onclick="window.location='index.php'" style="width:140px; height:40px; font-size:18px"/>
  <?php 
  	$Query11Sv = mysql_query("SELECT table_status_sv FROM table_status WHERE table_status_table='".$_SESSION["table"]."'");
	$result11Sv = mysql_fetch_array($Query11Sv);
	
  if($result11Sv['table_status_sv']=='0'){ ?>
  <input name="service" type="button" id="service" value="คิดค่าบริการ" onclick="javascript:if(confirm('ยืนยันการคิดค่าบริการ')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=service';}" style="width:140px; height:40px; font-size:18px" 
  <?php 	
  	$Query15s = mysql_query("SELECT member.memtype_id as memtype_id FROM member_non_session,member WHERE member.member_code = member_non_session.member_non_session_code AND  member_non_session.member_non_session_table ='".$_SESSION["table"]."'");
	$result2s31 = mysql_fetch_array($Query15s);
	
		switch ($result2s31['memtype_id']){
			case '5':
				echo "disabled";
				break;
			case '6':
				echo "disabled";
				break;
			case '7':
				echo "disabled";
				break;
		}
			
	?> />
  <?php } else if($result11Sv['table_status_sv']=='1') { ?>
  <input name="service" type="button" id="service" value="ยกเลิกค่าบริการ" onclick="javascript:if(confirm('ยืนยันการยกเลิกค่าบริการ')==true){window.location='<?=$_SERVER['PHP_SELF'];?>?action=noservice';}" style="width:140px; height:40px; font-size:18px"/>
    <?php } ?>
    </td>
    <td><table width="100%" border="0" align="right" cellpadding="1" cellspacing="0">
  <tr>
    <td align="right"><input name="sum" type="text" id="sum" style="width:200px; text-align:right; height:50px; font-size:50px; background-color:#FFF; color: red" value="<?="".number_format($strTotal, 2,'.',',');?>" readonly="readonly"/></td>
  </tr>
  <tr>
    <td align="right"><form name="receivemoney" id="receivemoney" method="post" action="<?=$_SERVER['PHP_SELF'];?>?action=receivem"><font color="#FF0000">&nbsp;
                    </font><font color="#000">รับเงิน : </font><input name="show" type="text" id="show" style="width:120px; text-align:right; height:25px; font-size:25px; color:#1859cd"  value="<?php 

if($receiverow["receive_money"]!=""){
 echo number_format($receiverow["receive_money"], 2,'.',','); 
}
 ?>" maxlength="10" autocomplete="off"/></form></td>
  </tr>
  <tr>
    <td align="right"><font color="#000">ทอนเงิน : </font><input name="show2" type="text" id="show2" style="width:120px; text-align:right; height:25px; font-size:25px; color: #1859cd" value="<?php 
if($receiverow["receive_money"]!='0'){
$aa = $receiverow["receive_money"] - $strTotal;
} else {
$aa = "0";
}
echo "".number_format($aa, 2,'.',',');?>" readonly="readonly"/></td>
  </tr>
</table></td>
  </tr>
</table>
</div>
</div>
</body>
</html>
